// pages/goods-details/index.js
const WXAPI = require('apifm-wxapi')
const CONFIG = require('../../config.js')
const Tools=require('../../utils/tools')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pid:"",
    basicInfo:{},
    pics:[],
    pics2:[],
    content:"",
    nodes:[],
    popflag:false,
    buyNum:1,
    properties:[],
    cartnum:0
  },
  addcart(){//购物车弹出层
    this.setData({
      popflag:true
    })
  },
  async sureadd(){//确认加入购物车
    var  token=wx.getStorageSync('token')
    console.log(token)
    var goodsId=this.data.basicInfo.id;
    console.log(goodsId)
    const sku = []
    if (this.data.properties) {
      this.data.properties.forEach(p => {
        sku.push({
          optionId:p.id,
          optionValueId:p['childsCurGoods'][0].id
        })
      })
    }
   console.log( this.data.buyNum)
   console.log(sku)
  // 添加购物车
    var res= await WXAPI.shippingCarInfoAddItem(token, goodsId, this.data.buyNum, sku)  
    console.log(res)
      if(res.code===0){
        this.setData({
          cartnum:res.data.number
        })
        wx.showToast({
          title: '添加购物车成功',
          duration:1000
        })
        Tools.showTabBarBadge()
        this.setData({
          popflag:false
        })
      }
  
  },
  /**
   * 生命周期函数--监听页面加载
   */
  plus(){
    console.log(111)
    var num=this.data.buyNum;

    this.setData({
      buyNum:++num
    })
  },
  min(){
    var num=this.data.buyNum
    if(this.data.buyNum<1){
        wx.showToast({
          title: '不能减小数量',
          duration:1000
        })
    }else{
      this.setData({
        buyNum:--num
      })
    }
    
  },
  share(){

  },
  yulan(e){

    console.log(e.currentTarget.dataset.index)
      wx.previewImage({
        urls: this.data.pics2,
        current:this.data.pics2[e.currentTarget.dataset.index]
      })
  },
  onLoad: function (options) { 
    WXAPI.init(CONFIG.subDomain)
    // 页面传参 在options中获取
      this.setData({
        pid:options.pid
      })
      // 请求详情页数据
      console.log(options.pid)
    
      wx.request({
        url: 'https://api.it120.cc/ljf/shop/goods/detail',
        method:"get",
        // header:{
        //   "Content-type":"application/json",
        //   "Content-type":"application/x-www-form-urlencoded"
        // },
        data:{
          id:options.pid
        },
        success:(res)=>{
          console.log(res)
          this.setData({
            basicInfo:res.data.data.basicInfo,
            pics:res.data.data.pics,
            pics2:res.data.data.pics2,
            content:res.data.data.content,
            properties:res.data.data.properties
          })
        }
      })
      this.loaddetail()
      this.loadpingjia()

  },
  async loaddetail(){
    // console.log(this.data.pid)
    // const goodsDetailRes = await WXAPI.goodsDetail(this.data.pid)
    // console.log(goodsDetailRes)
    // this.setData({
    //   basicInfo:goodsDetailRes.data.basicInfo,
    //   pics:goodsDetailRes.data.pics,
    //   pics2:goodsDetailRes.data.pics2,
    //   content:goodsDetailRes.data.content,
    //   properties:goodsDetailRes.data.properties
    // })
  },
  async loadpingjia(){
      var  res= await WXAPI.goodsReputation({goodsId:this.data.pid})
      console.log(res)

  },
  onShareAppMessage(e){ //分享时候出发
    
    console.log(e)
    return{
    title: '自定义转发标题',
    path:'/pages/goods-details/index'
    }
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }

 
})